<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/19
 * Time: 下午8:12
 */

class ModBroadcast extends Model {

    protected $table = 'broadcast';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbOss;
    }

    public function select($params){
        $where  = ' WHERE platform=:platform AND game=:game ';
        $args   = array(
            ':platform' => 'test',
            ':game'     => $params['g']
        );

//         if(!empty($params['server'])){
//             $where .= ' AND server_id = :server ';
//             $args[':server'] = $params['server'];
//         }

        if(!empty($params['server'])){
        	$where .= ' AND FIND_IN_SET("'.$params['server'].'",server_id) ';        	
        }
        
        
        if($params['begin'] && $params['end']){
        	$begin = strtotime($params['begin'] . ' 00:00:00');
        	$end = strtotime($params['end'] . ' 23:59:59');
        	$where  .= " AND create_time BETWEEN ".$begin. " AND  ".$end;
        }
        
        if($params['begin'] != '' && $params['end'] == ""){
        	$begin = strtotime($params['begin'] . ' 00:00:00');
        	$end = time();
        	$where  .= " AND create_time BETWEEN ".$begin. " AND  ".$end;
        }
        
        if($params['begin'] == '' && $params['end'] != ""){
        	$params['begin'] = date('Y-m-01');
        	$begin = strtotime($params['begin'] . ' 00:00:00');
        	$end = strtotime($params['end'] . ' 23:59:59');
        	$where  .= " AND create_time BETWEEN ".$begin. " AND  ".$end;
        }
        
        if(!empty($params['create_user'])){
            $where .= ' AND create_user = :create_user ';
            $args[':create_user'] = $params['create_user'];
        }

        $sql = "SELECT * FROM {$this->table} {$where} ORDER BY create_time DESC";
        $result = $this->getDbConnection()->getAll($sql, $args);
        return $result;
    }
} 